from django.contrib import admin
from .models import Conversation, Message, RetrievedDocument, QuestionFeedback


@admin.register(Conversation)
class ConversationAdmin(admin.ModelAdmin):
    list_display = ['title', 'user', 'knowledge_base', 'is_archived', 'created_at']
    list_filter = ['is_archived', 'user', 'knowledge_base']
    search_fields = ['title', 'user__username']
    date_hierarchy = 'created_at'


@admin.register(Message)
class MessageAdmin(admin.ModelAdmin):
    list_display = ['conversation', 'role', 'content_preview', 'tokens', 'model', 'created_at']
    list_filter = ['role', 'model']
    search_fields = ['content', 'conversation__title']
    date_hierarchy = 'created_at'

    def content_preview(self, obj):
        return obj.content[:50] + '...' if len(obj.content) > 50 else obj.content

    content_preview.short_description = '消息内容'


@admin.register(RetrievedDocument)
class RetrievedDocumentAdmin(admin.ModelAdmin):
    list_display = ['document', 'message', 'relevance_score', 'chunk_index', 'created_at']
    list_filter = ['document']
    search_fields = ['content', 'document__title']


@admin.register(QuestionFeedback)
class QuestionFeedbackAdmin(admin.ModelAdmin):
    list_display = ['user', 'message', 'rating', 'created_at']
    list_filter = ['rating', 'user']
    search_fields = ['comment', 'user__username']
    date_hierarchy = 'created_at'
